package com.gitee.huanminabc.fastboot.web.validation;

import com.gitee.huanminabc.fastboot.exception.ValidationException;
import com.gitee.huanminabc.fastboot.web.annotation.validation.NotEmpty;

import java.lang.annotation.Annotation;
import java.util.Collection;

/**
 * @description:
 * @author: huanmin
 * @create: 2025-04-02 15:11
 **/
public class NotEmptyValidationVerify implements ValidationVerify {
    @Override
    public Class<?> getAnnotation() {
        return NotEmpty.class;
    }
    @Override
    public void verify(Object value, Annotation annotation,String gprmc) {
        NotEmpty notEmpty = (NotEmpty) annotation;
        if (value == null) {
            throw new ValidationException(this.gprmcMerge(gprmc, notEmpty.message()));
        }
        //如果是Collection类型
        if (value instanceof Collection) {
            Collection<?> collection = (Collection<?>) value;
            if (collection.isEmpty()) {
                throw new ValidationException(this.gprmcMerge(gprmc,notEmpty.message()));
            }
        }
        //如果是数组类型
        else if (value.getClass().isArray()) {
            int length = java.lang.reflect.Array.getLength(value);
            if (length == 0) {
                throw new ValidationException(this.gprmcMerge(gprmc,notEmpty.message()));
            }
        }
        //如果是Map类型
        else if (value instanceof java.util.Map) {
            java.util.Map<?, ?> map = (java.util.Map<?, ?>) value;
            if (map.isEmpty()) {
                throw new ValidationException(this.gprmcMerge(gprmc,notEmpty.message()));
            }
        }
    }
}
